package wef.PW.module.command;

import wef.PW.module.Initialization;
import wef.PW.module.model.APDU;
import wef.PW.utils.RewardUtils;
import wef.PW.utils.StrUtils;


public class CommandImpl implements Command{

    @Override
    public boolean generalCallCommand(int adr) {

        int receiveNum = Initialization.plcl.getReceiveNum();
        int sendNum = Initialization.plcl.getSendNum();

        APDU apdu = new APDU();
        apdu.setAPDUL("0E");
        String CLDNA = RewardUtils.getNum(sendNum);
        apdu.setCLDN1(CLDNA.substring(0, 2));
        apdu.setCLDN2(CLDNA.substring(2, 4));
        String CLDNAB = RewardUtils.getNum(receiveNum);
        apdu.setCLDN3(CLDNAB.substring(0, 2));
        apdu.setCLDN4(CLDNAB.substring(2, 4));
        String ASDU = apdu.getReward();
        String TYP = "64";  // 类型标识符
        String VSQ = "01"; // 可变帧长限定词
        String COT = "0600"; // 传送原因
        String ADR =  StrUtils.strFlip(StrUtils.strPolish(Integer.toHexString(adr), 4)); // 公共地址
        String InfoAdr = "000000"; // 信息对象地址
        String INOT = "14"; // 召唤限定词
        String reward = ASDU + TYP + VSQ + COT + ADR + InfoAdr + INOT; // 拼接报文
        System.out.println("总召唤确认报文:" + reward);
        if (Initialization.plcl.getRewardReceive().SendReward(reward)){
            //发送成功发送序号 自增1
            Initialization.plcl.setSendNum(sendNum + 1);
            return true;
        }
        return false;
    }

    @Override
    public boolean testCommand() {
        if (Initialization.plcl.getRewardReceive().SendReward("680483000000"))
            return true;
        return false;
    }

    @Override
    public boolean sRewardCommand() {
        int receiveNum = Initialization.plcl.getReceiveNum();
        String reward = "68040100" + RewardUtils.getNum(receiveNum);;
        Initialization.plcl.getRewardReceive().SendReward(reward);
        return false;
    }
}
